package com.ljh.mall.order.interceptor;

import com.ljh.common.constant.AuthConstant;
import com.ljh.common.vo.MemberVO;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author ：刘剑鸿
 * @date ：Created in 2023/4/20 21:42
 * @description：
 * @modified By：
 * @version: $
 */
public class AuthInterceptor implements HandlerInterceptor {

    public static ThreadLocal threadLocal = new ThreadLocal();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object attribute = session.getAttribute(AuthConstant.AUTH_SESSION_REDIS);
        if (attribute != null){
           MemberVO memberVO = (MemberVO)attribute;
            threadLocal.set(memberVO);
            return true;
        }
        session.setAttribute(AuthConstant.AUTH_SESSION_MSG,"请先登录");
        response.sendRedirect("http://localhost:30000/login.html");
        return false;
    }
}
